// ignore_for_file: constant_identifier_names, prefer_typing_uninitialized_variables, avoid_print

import '../dao/login_dao.dart';

enum HttpMethod { GET, POST, DELETE }

/// 基础请求:
abstract class BaseRequest {
  // curl -X GET "https://api.devio.org/uapi/test/test?requestPrams=ddd" -H "accept: */*" -H "course-flag: ZmEtMjAyMS0wNC0xMiAyMToyMjoyMC1mYQ==fa"

  var pathParams;
  var useHttps = false;
  String authority() {
    return "120.25.126.125:7788";
  }

  HttpMethod httpMethod();
  bool userJsonAppcalition();
  String path();
  bool needLogin();

  String url() {
    Uri uri;
    var pathStr = path();
    if (pathParams != null) {
      if (path().endsWith("/")) {
        pathStr = "${path()}$pathParams";
      } else {
        pathStr = "${path()}/$pathParams";
      }
    }

    // http和https切换:
    if (useHttps) {
      uri = Uri.https(authority(), pathStr, params);
    } else {
      uri = Uri.http(authority(), pathStr, params);
    }

    if (needLogin()) {
      // 给需要登录页的接口携带登录令牌:
      addHeader(LoginDao.BOARDING_PASS, LoginDao.getBoardingPass());
    }

    print('url: ${uri.toString()}');

    return uri.toString();
  }

  // 拼接的参数:
  Map<String, String> params = {};

  BaseRequest add(String key, Object value) {
    params[key] = value.toString();
    return this;
  }

  // json
  Map<String, dynamic> jsonData = {};
  BaseRequest addJson(String key, Object value) {
    jsonData[key] = value;
    return this;
  }

  // 鉴权:
  Map<String, String> header = {};

  BaseRequest addHeader(String key, dynamic value) {
    header[key] = value.toString();
    return this;
  }

  setJsonHeader() {
    jsonData["Content-Type"] = "application/json; charset=utf-8";
  }
}
